package com.hns.can.master.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;

import com.hns.can.master.vo.DeviceParamVo;
import com.hns.frame.processor.TaskProcessor;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.JsonUtil;
import com.hns.tool.pub.PubUtil;
@Service
public class DeviceParamService extends CommonService implements TaskProcessor{
	
	private static final Logger logger = LoggerFactory.getLogger(DeviceParamService.class);
	
	@Autowired
    private TaskExecutor taskExecutor;
	
	private String canMsg;
	
	private ArrayList<DeviceParamVo> voList;

	public List<DeviceParamVo> getDeviceParamSelection(DeviceParamVo params) {
		List<DeviceParamVo> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("deviceParam.getDeviceParamSelection", params), DeviceParamVo.class);
		return list;
	}

	public DeviceParamVo getReltivEqmtCd(DeviceParamVo params) {
		// TODO Auto-generated method stub
		DeviceParamVo vo = null;
		List<DeviceParamVo> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("deviceParam.getReltivEqmtCd", params), DeviceParamVo.class);
		if(PubUtil.isNotEmpty(list)){
			vo = list.get(0);
		}
		return vo;
	}

	
	public ArrayList<DeviceParamVo> getDevParamData(DeviceParamVo params) {
		try{
			canMsg = "{'carId':'" + params.getCarId() + "', 'devId':'" + params.getEqmtCd() + "', 'type': '" + params.getType() + "', 'paramID':'"+params.getParamType()+"'}";
			this.process();
		}catch(Exception e){
			e.printStackTrace();
		}
		return voList;
	}
	

	@Override
	public void process() throws Exception {
		// TODO Auto-generated method stub
		taskExecutor.execute(new Runnable() {
            public void run() {
                WebServiceBSService webServiceBSService = new WebServiceBSService();
                Object[] result;
                try {
                	result=webServiceBSService.sendToCommModel(canMsg, "1F");
                	 String jsonStr = (result != null ? (String) result[0] : "");
                	 Map jsonMap = JsonUtil.getMapFromJsonString(jsonStr);
                	 JSONObject head = (JSONObject) jsonMap.get("head");
                	 if(head!=null){
                		 String stateCode = head.get("state_code").toString();
                         String stateDesc = head.get("state_desc").toString();
                         if("0".equals(stateCode)&&stateDesc.equals("成功")){
//                        	 stateCode="UPST0300";
//                        	 stateDesc="UPSM0105";
                        	 JSONObject body = (JSONObject) jsonMap.get("body");
                        	 logger.error(body.toString());
                        	 JSONObject[] paramList = (JSONObject[])body.get("paramList");
                        	 if(PubUtil.isNotEmpty(paramList)){
                        		 voList = new ArrayList<DeviceParamVo>();
                        		 for(int i=0;i<paramList.length;i++){
                        			 DeviceParamVo vo = new DeviceParamVo();
                        			 vo.setParamID((String)paramList[i].getString("paramID"));
                        			 vo.setParamValue((String)paramList[i].getString("paramValue"));
                        			 voList.add(vo);
                        		 }
                        	 }
                         }
                	 }
				} catch (Exception e) {
					e.printStackTrace();
				}
            }
        });	
	}
	
}
